package com.hylanda.gate.prs;/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//import com.hylanda.reidx.source.DigData;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//下面是引用到JDOM中的类

/**
 *
 * @author pangxiaoxi
 */
public class StandardDate{
    static final String m_EnMonth[] = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
    static final String m_ChMonth[] = {"一","二","三","四","五","六","七","八","九","十","十一","十二"};
    public final static long _dateMinMILL = 346991477529L; // 1980-12-30 0:0:0,000
    public final static long _dateMinSec = 346953600L;// 1980-12-30 0:0:0,000
    public final static long _dateMaxSec = 4133779200L;// 2100-12-30 0:0:0,000
    //    static public SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    /* 自动识别处理以下格式
      * 日期
      * yyyy-mm-dd 
      * yy-mm-dd  
      * yyyy年mm月dd(日|号) 
      * yyyy年(一|二|三|四|五|六|七|八|九|十|十一|十二)月dd日
      * yyyy年(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC),dd
      * 时间
      * HH:MM:SS
      * HH(时|点)MM分SS秒
      * A.M.|a.m.|AM|am|上午|P.M|p.m.|PM|pm|下午
      * e.g.
      * 2011年09月13日02:58 
      * 2011.9.13 10:26
      * 2011-09-13 11:14:45
      * 2011-09-13 09:49
      * 2011年09月13日 14:27 PM
      */
//     public static String GetStandardDateString(String sDate){
//         Date d = getStandardDate(sDate.trim());
//         if(d != null){
//        	 synchronized(dateFormat){
//        		 return dateFormat.format(d);	 
//        	 }
//         }
//         return null;
//     }


    public static DateWithPrecision GetStandardDate(String s){
        if(s== null || s.isEmpty() )
            return null;
        try{
            String sDate = replaceSpaceAtZhEn(s);
            int nIndex = 0 ;
            int nValueType = -1;  //数值的类型.0:数字;1:英文;2:中文
            int len = sDate.length();
            int y=0,m=0,d=0;
            int H=-1,M=-1,S=-1;
            String sVal = "";
            while(nIndex < len){
                char ch = sDate.charAt(nIndex);
                if(Character.isDigit(ch) ){
                    sVal += ch;
                    nValueType = 0;
                }else if(isAlpha(ch)){
                    sVal += ch;
                    nValueType = 1;
                }
                else if("一二三四五六七八九十".indexOf(ch) != -1){
                    sVal += ch;
                    nValueType = 2;
                }else if("前".indexOf(ch) != -1){
                    return null;
                }else{ //是分割符号,判断
                    if(sVal.length() == 0){
                        nIndex ++;
                        continue;
                    }
                    if("-./\\年".indexOf(ch) != -1 && y ==0){
                        y = Integer.parseInt(sVal);
/*                         if(y >=70 && y <=99)
                             y += 1900;
                         else if(y >= 0 && y <= 38)
                             y+= 2000;
                         else if(y < 1970 || y > 2038)
                             y = 0; //invalid 不合法
                             */
                    }else if("-./\\月".indexOf(ch) != -1 && m == 0){
                        int mm = Integer.parseInt(sVal);
                        if(mm >= 1 && mm <= 12)
                            m = mm;
                        else {
                            m = ParseEnMonth(sVal);
                            if(m == 0){
                                m = ParseChMonth(sVal);
                            }
                        }
                    }else if(" 日号".indexOf(ch) != -1 && d == 0){
                        int dd = Integer.parseInt(sVal);
                        if(dd >=1 && dd <=31){
                            d = dd;
                            if(m == 0 && y > 0 && y <=12 ){
                                m = y;
                                y = guessYear(m,d);//Calendar.getInstance().get(Calendar.YEAR);
                            }
                        }
                        else{
                            d = ParseChDay(sVal);
                        }
                    }else if(" :时点：".indexOf(ch) != -1 && H == -1){
                        H = Integer.parseInt(sVal);

                    }
                    else if(" :分：".indexOf(ch) != -1 && M == -1){
                        M = Integer.parseInt(sVal);
                    }
                    else if(ch == ':' || ch == ' '|| ch == '.' || nIndex +1 == len){
                        if(H == -1)
                            H = Integer.parseInt(sVal);
                        else if(M == -1)
                            M = Integer.parseInt(sVal);
                        else if(S == -1)
                            S = Integer.parseInt(sVal);
                    }else {
                        sVal= "";
                    }
                    sVal = "";
                }
                nIndex ++;
            }
            int nLast = -1; //  修改0 => -1
            if(sVal.length() > 0){
                if(nValueType == 0){
                    nLast = Integer.parseInt(sVal);
                }else if(nValueType == 1){
                    sVal = sVal.toLowerCase();
                    if("pmp.m".indexOf(sVal) != -1){
                        if(H > 0 && H <= 12)
                            H += 12;
                    }
                }
            }
            int prec = 0;
            if(y == 0){
                if(sVal.length() == 8 ){ //20120104
                    d = nLast % 100;
                    m = (nLast /100)%100;
                    y = nLast / 10000;
                    H = 0; M = 0; S=0;
                    prec = DateWithPrecision.PREC_DAY;
                }else if(m != 0 && d != 0){
                    y = guessYear(m,d);
                    if(H == -1){
                        H = 0; M=0; S= 0;
                    }else if(M == -1){
                        M=0; S= 0;
                    }else if(S == -1){
                        S = 0;
                    }
                }else {
                    return null;
                }
            }else if(d == 0){
                if(nLast == -1){
                    prec = DateWithPrecision.PREC_MONTH;
                    d = 1;
                }else{
                    d = nLast;
                    if(m == 0 && y > 0 && y <=12 ){
                        m = y;
                        y = guessYear(m,d);//Calendar.getInstance().get(Calendar.YEAR);
                    }
                    prec = DateWithPrecision.PREC_DAY;
                }
                H = 0; M = 0; S=0;
            }
            else if(H == -1){
                if(nLast == -1){
                    H = 0;
                    prec = DateWithPrecision.PREC_DAY;
                }else{
                    H = nLast;
                    prec = DateWithPrecision.PREC_HOUR;
                }
                M = 0; S=0;
            }else if(M == -1){
                if(nLast == -1){
                    M = 0 ;
                    prec = DateWithPrecision.PREC_HOUR;
                }else{
                    M = nLast;
                    prec = DateWithPrecision.PREC_MINUTE;
                }
                S=0;

            }else if(S ==-1){
                if(nLast == -1){
                    S = 0;
                    prec = DateWithPrecision.PREC_MINUTE;
                }else{
                    S = nLast;
                    prec = DateWithPrecision.PREC_SECOND;
                }
            }else{
                prec = DateWithPrecision.PREC_SECOND;
            }
            Calendar cale = Calendar.getInstance();
            if(y >=70 && y <=99)
                y += 1900;
            else if(y >= 0 && y <= 38)
                y+= 2000;
            else if(y < 1970 || y > 2038)
                y = 0;
            cale.set(y,m-1,d,H,M,S);
            cale.set(Calendar.MILLISECOND,0);
            DateWithPrecision dt = new DateWithPrecision(cale.getTime());
            dt.setPrecision(prec);
            return dt;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }
    private static DateWithPrecision GetStandardDateNeedYear(String s){
        if(s== null || s.isEmpty() )
            return null;
        try{
            String sDate = replaceSpaceAtZhEn(s);
            int nIndex = 0 ;
            int nValueType = -1;  //数值的类型.0:数字;1:英文;2:中文
            int len = sDate.length();
            int y=0,m=0,d=0;
            int H=-1,M=-1,S=-1;
            String sVal = "";
            while(nIndex < len){
                char ch = sDate.charAt(nIndex);
                if(Character.isDigit(ch) ){
                    sVal += ch;
                    nValueType = 0;
                }else if(isAlpha(ch)){
                    sVal += ch;
                    nValueType = 1;
                }
                else if("一二三四五六七八九十".indexOf(ch) != -1){
                    sVal += ch;
                    nValueType = 2;
                }else if("前".indexOf(ch) != -1){
                    return null;
                }else{ //是分割符号,判断
                    if(sVal.length() == 0){
                        nIndex ++;
                        continue;
                    }
                    if("-./\\年".indexOf(ch) != -1 && y ==0){
                        y = Integer.parseInt(sVal);
/*                         if(y >=70 && y <=99)
                             y += 1900;
                         else if(y >= 0 && y <= 38)
                             y+= 2000;
                         else if(y < 1970 || y > 2038)
                             y = 0; //invalid 不合法
                             */
                    }else if("-./\\月".indexOf(ch) != -1 && m == 0){
                        int mm = Integer.parseInt(sVal);
                        if(mm >= 1 && mm <= 12)
                            m = mm;
                        else {
                            m = ParseEnMonth(sVal);
                            if(m == 0){
                                m = ParseChMonth(sVal);
                            }
                        }
                    }else if(" 日号".indexOf(ch) != -1 && d == 0){
                        int dd = Integer.parseInt(sVal);
                        if(dd >=1 && dd <=31){
                            d = dd;
                            if(m == 0 && y > 0 && y <=12 ){
                                m = y;
                                //y = guessYear(m,d);//Calendar.getInstance().get(Calendar.YEAR);
                                return null;
                            }
                        }
                        else{
                            d = ParseChDay(sVal);
                        }
                    }else if(" :时点：".indexOf(ch) != -1 && H == -1){
                        H = Integer.parseInt(sVal);

                    }
                    else if(" :分：".indexOf(ch) != -1 && M == -1){
                        M = Integer.parseInt(sVal);
                    }
                    else if(ch == ':' || ch == ' '|| ch == '.' || nIndex +1 == len){
                        if(H == -1)
                            H = Integer.parseInt(sVal);
                        else if(M == -1)
                            M = Integer.parseInt(sVal);
                        else if(S == -1)
                            S = Integer.parseInt(sVal);
                    }else {
                        sVal= "";
                    }
                    sVal = "";
                }
                nIndex ++;
            }
            int nLast = -1; //  修改0 => -1
            if(sVal.length() > 0){
                if(nValueType == 0){
                    nLast = Integer.parseInt(sVal);
                }else if(nValueType == 1){
                    sVal = sVal.toLowerCase();
                    if("pmp.m".indexOf(sVal) != -1){
                        if(H > 0 && H <= 12)
                            H += 12;
                    }
                }
            }
            int prec = 0;
            if(y == 0){
                if(sVal.length() == 8 ){ //20120104
                    d = nLast % 100;
                    m = (nLast /100)%100;
                    y = nLast / 10000;
                    H = 0; M = 0; S=0;
                    prec = DateWithPrecision.PREC_DAY;
                }else if(m != 0 && d != 0){
                    //y = guessYear(m,d);
//                	 if(H == -1){
//                		 H = 0; M=0; S= 0;
//                	 }else if(M == -1){
//                		 M=0; S= 0;
//                	 }else if(S == -1){
//                		 S = 0;
//                	 }
                    return null;
                }else {
                    if(nLast>1920 && nLast<2038){
                        prec = DateWithPrecision.PREC_YEAR;
                        Calendar cale = Calendar.getInstance();
                        cale.set(nLast,0,1,0,0,0);
                        cale.set(Calendar.MILLISECOND,0);
                        DateWithPrecision dt = new DateWithPrecision(cale.getTime());
                        dt.setPrecision(prec);
                        return dt;
                    }else{
                        return null;
                    }
                }
            }else if(d == 0){
                if(nLast == -1){
                    prec = DateWithPrecision.PREC_MONTH;
                }else{
                    d = nLast;
                    if(m == 0 && y > 0 && y <=12 ){
                        m = y;
                        //y = guessYear(m,d);//Calendar.getInstance().get(Calendar.YEAR);
                        return null;
                    }
                    prec = DateWithPrecision.PREC_DAY;
                }
                H = 0; M = 0; S=0;
            }
            else if(H == -1){
                if(nLast == -1){
                    H = 0;
                    prec = DateWithPrecision.PREC_DAY;
                }else{
                    H = nLast;
                    prec = DateWithPrecision.PREC_HOUR;
                }
                M = 0; S=0;
            }else if(M == -1){
                if(nLast == -1){
                    M = 0 ;
                    prec = DateWithPrecision.PREC_HOUR;
                }else{
                    M = nLast;
                    prec = DateWithPrecision.PREC_MINUTE;
                }
                S=0;

            }else if(S ==-1){
                if(nLast == -1){
                    S = 0;
                    prec = DateWithPrecision.PREC_MINUTE;
                }else{
                    S = nLast;
                    prec = DateWithPrecision.PREC_SECOND;
                }
            }
            Calendar cale = Calendar.getInstance();
            if(y >=20 && y <=99)
                y += 1900;
            else if(y >= 0 && y <= 38)
                y+= 2000;
            else if(y < 1920 || y > 2038)
                y = 0;
            if(m==0) m=1;
            if(d==0) d=1;
            cale.set(y,m-1,d,H,M,S);
            cale.set(Calendar.MILLISECOND,0);
            DateWithPrecision dt = new DateWithPrecision(cale.getTime());
            dt.setPrecision(prec);
            return dt;
        }catch(Exception ex){
            System.out.println(ex.toString());
        }
        return null;
    }
    /* 处理以下的格式
     * 35分钟前
       2小时前
       半小时前
       一小时前
        今天 13:15
        7月10日 00:00
        12:00 AM
        7月10日
        3月前
*/
//     public static String GetStandardDateString(String sDate,Date baseTime){
//    	 Date d = null;
//    	 try{
//    		 d = getStandardDate(sDate,baseTime);
//    	 }catch(Exception ex){
//
//    	 }
//    	 synchronized(dateFormat){
//    		 return d == null ? null : dateFormat.format(d);
//    	 }
//     }

    public static Date GetStandardDate(String sDate,Date baseTime){
        Date d = null;
        try {
            if(sDate!=null){
                d = GetStandardDate(sDate.trim());
            }else{
                return d;
            }
            if(d != null){
                return d;
            }else if(baseTime != null){
                int pos = -1;
                if((pos= sDate.indexOf("分钟前"))> 0){
                    float nMin = extractDigit(sDate,pos);
                    long newTime = baseTime.getTime() - (long)(nMin*60000L);
                    d = new Date(newTime);
                }else if((pos= sDate.indexOf("小时前"))> 0){
                    float nHour = extractDigit(sDate,pos);
                    long newTime = baseTime.getTime() - (long)(nHour*60*60000L);
                    d = new Date(newTime);
                }else if((pos= sDate.indexOf("天前"))> 0){
                    float nDay = extractDigit(sDate,pos);
                    long newTime = baseTime.getTime() - (long)(nDay*24*60*60000L);
                    d = new Date(newTime);
                }else if((pos= sDate.indexOf("月前"))> 0){
                    float nMonth = extractDigit(sDate,pos);
                    long newTime = baseTime.getTime() - (long)(nMonth*30*24*60*60000L);
                    d = new Date(newTime);
                }else if((pos= sDate.indexOf("年前"))> 0){
                    float nYear = extractDigit(sDate,pos);
                    long newTime = baseTime.getTime() - (long)(nYear*365*24*60*60000L);
                    d = new Date(newTime);
                }else if((pos = sDate.indexOf("今天")) >=0 ){
                    String s = sDate.substring(pos + 2).trim();
                    String[] tmp = s.split(":");
                    if(tmp.length >= 2 ){
                        int nHour = Integer.parseInt(tmp[0]);
                        int nMin = Integer.parseInt(tmp[1]);
                        Calendar cale = Calendar.getInstance();
                        cale.setTime(baseTime);
                        cale.set(Calendar.HOUR_OF_DAY, nHour);
                        cale.set(Calendar.MINUTE, nMin);
                        if(tmp.length > 2){
                            int nSec = Integer.parseInt(tmp[2]);
                            cale.set(Calendar.SECOND, nSec);
                        }
                        cale.set(Calendar.MILLISECOND,0);
                        d = cale.getTime();
                    }
                }else if((pos = sDate.indexOf("昨天")) >=0 ){
                    String s = sDate.substring(pos + 2).trim();
                    String[] tmp = s.split(":");
                    if(tmp.length >= 2 ){
                        int nHour = Integer.parseInt(tmp[0]);
                        int nMin = Integer.parseInt(tmp[1]);
                        Calendar cale = Calendar.getInstance();
                        Date dd = new Date(baseTime.getTime() - (24*60*60000L)); //一天前
                        cale.setTime(dd);
                        cale.set(Calendar.HOUR_OF_DAY, nHour);
                        cale.set(Calendar.MINUTE, nMin);
                        if(tmp.length > 2){
                            int nSec = Integer.parseInt(tmp[2]);
                            cale.set(Calendar.SECOND, nSec);
                        }
                        cale.set(Calendar.MILLISECOND,0);
                        d = cale.getTime();
                    }
                }else if((pos = sDate.indexOf("前天")) >=0 ){
                    String s = sDate.substring(pos + 2).trim();
                    String[] tmp = s.split(":");
                    if(tmp.length >= 2 ){
                        int nHour = Integer.parseInt(tmp[0]);
                        int nMin = Integer.parseInt(tmp[1]);
                        Calendar cale = Calendar.getInstance();
                        Date dd = new Date(baseTime.getTime() - (2*24*60*60000L)); //2天前
                        cale.setTime(dd);
                        cale.set(Calendar.HOUR_OF_DAY, nHour);
                        cale.set(Calendar.MINUTE, nMin);
                        if(tmp.length > 2){
                            int nSec = Integer.parseInt(tmp[2]);
                            cale.set(Calendar.SECOND, nSec);
                        }
                        cale.set(Calendar.MILLISECOND,0);
                        d = cale.getTime();
                    }
                }
                else{
                    int nIndex = 0 ;
                    String sVal = "";
                    int yy=-1,mm = -1, dd = -1, H=-1 , M=-1, S = 0;
                    int nAMPM = -1; // 0 am， 1 pm
                    boolean bAlpha = false;
                    while(nIndex < sDate.length()){
                        char ch = sDate.charAt(nIndex);
                        if(Character.isDigit(ch) ){
                            sVal += ch;
                        }else if(isAlpha(ch)){
                            sVal += ch;
                            bAlpha = true;
                        }else{ //是分割符号,判断
                            if(sVal.length() == 0){
                                nIndex ++;
                                continue;
                            }else if("年".indexOf(ch) != -1){
                                yy = parseIntValue(sVal,-1);
                                sVal = "";
                            }else if("月".indexOf(ch) != -1){
                                mm = parseIntValue(sVal,-1);
                                sVal = "";
                            }else if("日".indexOf(ch) != -1){
                                dd = parseIntValue(sVal,-1);
                                sVal = "";
                            }else if(":：时点".indexOf(ch) != -1 && H == -1){
                                H = parseIntValue(sVal,-1);
                                sVal = "";
                            }
                            else if(":：分 ".indexOf(ch) != -1 && M == -1|| nIndex +1 == sDate.length()){
                                M = Integer.parseInt(sVal);
                                sVal = "";
                            }
                            else if(ch == ':' || ch == ' '|| nIndex +1 == sDate.length()){
                                if(bAlpha){
                                    if("am".equalsIgnoreCase(sVal)){
                                        nAMPM = 0;
                                    }else if("pm".equalsIgnoreCase(sVal)){
                                        nAMPM = 1;
                                    }
                                    bAlpha = false;
                                }else{
                                    if(H == -1)
                                        H = Integer.parseInt(sVal);
                                    else if(M == -1)
                                        M = Integer.parseInt(sVal);
                                    else if(S == -1)
                                        S = Integer.parseInt(sVal);
                                }
                                sVal = "";
                            }
                            sVal = "";
                        }
                        nIndex++;
                    }
                    if(!sVal.isEmpty()){
                        if(bAlpha){
                            nAMPM = checkAMorPM(sVal);
                        }else{
                            if(H == -1)
                                H = Integer.parseInt(sVal);
                            else if(M == -1)
                                M = Integer.parseInt(sVal);
                            else if(S == -1)
                                S = Integer.parseInt(sVal);
                        }
                        sVal = "";
                    }
                    Calendar cale = Calendar.getInstance();
                    cale.setTime(baseTime);
                    cale.set(Calendar.MILLISECOND,0);
                    if(nAMPM == 0){
                        cale.set(Calendar.AM_PM,Calendar.AM );
                    }else if(nAMPM == 1){
                        cale.set(Calendar.AM_PM,Calendar.PM );
                        if(H >0 && H < 12)
                            H += 12;
                    }
                    if(yy != -1 && yy > 1000 ){
                        cale.set(Calendar.YEAR, yy);
                    }
                    if(mm != -1 && mm > 0 && mm <= 12){
                        cale.set(Calendar.MONTH, mm - 1);
                    }
                    if(dd != -1 && dd >0 && dd <= 31){
                        cale.set(Calendar.DAY_OF_MONTH, dd);
                    }
                    if(H != -1){
                        cale.set(Calendar.HOUR_OF_DAY, H);
                    }
                    if(M != -1){
                        cale.set(Calendar.MINUTE, M);
                    }
                    if(S != -1){
                        cale.set(Calendar.SECOND, S);
                    }
                    d = cale.getTime();
                }
            }
        } catch(NumberFormatException e) {
        }
        return d;
    }
    private static int checkAMorPM(String sVal){
        int nAMPM = -1;
        if("am".equalsIgnoreCase(sVal)){
            nAMPM = 0;
        }else if("pm".equalsIgnoreCase(sVal)){
            nAMPM = 1;
        }
        return nAMPM;
    }
    private static int parseIntValue(String s, int defaultValue){
        try{
            return Integer.parseInt(s);
        }catch(Exception ex){
            return defaultValue;
        }
    }
    //提取字符串指定位置前的最后一组数字或'半,一,两
    private static float extractDigit(String sDate,int pos){
        String sVal = "";
        int index = 0;
        boolean isDigit = false;
        while(index < pos){
            char ch = sDate.charAt(index);
            if(Character.isDigit(ch)){
                if(!isDigit)
                    sVal = "";
                sVal += ch;
                isDigit = true;
            }else if(ch == '半'){
                return 0.5f;
            }else if(ch == '一'){
                return 1;
            }else if(ch == '二' || ch == '两'){
                return 2;
            }else{
                if(!isDigit)
                    sVal = "";
                else
                    isDigit = false;
            }
            index ++;
        }
        return sVal.isEmpty() ? 0:Integer.parseInt(sVal);
    }

    /*
     * 分析英文的月份,成功返回1-12,失败返回0
     */
    private static int ParseEnMonth(String sVal){
        String s = sVal.toUpperCase();
        for ( int i = 0 ;  i < m_EnMonth.length; i++){
            if(s.compareTo(m_EnMonth[i]) == 0){
                return i+1;
            }
        }
        return 0;
    }
    private static int ParseChMonth(String sVal){

        for ( int i = 0 ;  i < m_ChMonth.length; i++){
            if(sVal.compareTo(m_ChMonth[i]) == 0){
                return i+1;
            }
        }
        return 0;
    }
    //返回1~31,错误返回0
    private static int ParseChDay(String sVal){
        int len = sVal.length();
        if(len == 1){
            return "一二三四五六七八九十".indexOf(sVal) + 1;
        }else if(len >= 2){  //十一 ~十九,二十,三十
            if(sVal.charAt(0) == '十'){
                return 11+"一二三四五六七八九十".indexOf(sVal.charAt(1));
            }else if(sVal.charAt(1) == '十'){ //二十,三十
                if(sVal.charAt(0) == '二' ){
                    if(len == 2)
                        return 20;
                    else
                        return 20+ "一二三四五六七八九".indexOf(sVal.charAt(2));
                }
                else if(sVal.charAt(0) == '三'){
                    if(len == 2)
                        return 30;
                    else if(sVal.charAt(2) == '一')
                        return 31;
                }
            }
        }
        return 0;
    }
    public static boolean isAlpha(char ch){
        return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
    }
    //去除日期中，中英文之间的字符.
    public static String replaceSpaceAtZhEn(String s){
        StringBuilder sbuf = new StringBuilder();
        char last_char = ' ';
        for (int index =0 ; index < s.length() ; index ++){
            char ch = s.charAt(index);
            if(!Character.isWhitespace(ch)){
                sbuf.append(ch);
                last_char = ch;
            }else{
                if(index > 0 && index+1 < s.length()){
                    char left = s.charAt(index-1);
                    char right= s.charAt(index+1);
                    if(Character.isWhitespace(right) ) //空格的右边边还是空格，忽略掉
                        continue;
                    if(Character.isDigit(last_char) && (Character.isDigit(right)||isAlpha(right) ) ){
                        sbuf.append(' ');
                    }
                }

            }
        }
        return sbuf.toString();
    }

    public static int guessYear(int m,int d){
        int y = 0;
        if(m != 0 && d != 0){
            if(m < Calendar.getInstance().get(Calendar.MONTH) + 1  ){
                y = Calendar.getInstance().get(Calendar.YEAR);
            }else if(m == Calendar.getInstance().get(Calendar.MONTH) + 1 && d <= Calendar.getInstance().get(Calendar.DAY_OF_MONTH) ){
                y = Calendar.getInstance().get(Calendar.YEAR) ;
            }else{
                y = Calendar.getInstance().get(Calendar.YEAR) - 1;
            }
        }
        return y;
    }
}
